# @Time : 2022/7/19 10:07
# @Author : zhangzhihui
# @Email : mrzhangzh@gmail.com
from bs4 import BeautifulSoup
import urllib.parse
import base64


def deal_templates_html(html):
    html = urllib.parse.unquote(base64.b64decode(html).decode())
    soup = BeautifulSoup(html, 'lxml')
    # print('html', html)
    input_strs = soup.findAll(['input', 'select', 'textarea'])
    record_list = []
    for input_str in input_strs:
        # print(input_str, input_str.get('id'), input_str.get('type'))
        if input_str.get('type') == 'radio':
            record_list.append({"id": input_str.get('id', ''), "type": input_str.get('type'), "name": "radio_button"})
        else:
            record_list.append({"id": input_str.get('id', ''), "type": input_str.get('type')})

    return record_list


def deal_process_html(dict):
    routes = dict['route']
    lines = dict['line']
    routes_list = []
    pageSourceIds = []
    pageTargetIds = []
    for route in routes:
        routes_list.append(route['divId'])
    # routes_list.append('chart-server1')
    for line in lines:
        pageSourceIds.append(line['pageSourceId'])
        pageTargetIds.append(line['pageTargetId'])
    audit = []
    cf = []
    n = 1
    for route in routes_list:
        for line in lines:
            if line['pageTargetId'] == route:
                if route not in cf:
                    audit.append({'name': route, 'num': n})
                    cf.append(route)
                    if pageSourceIds.count(line['pageSourceId']) == 1:
                        n += 1
                    else:
                        pageSourceIds.remove(line['pageSourceId'])
    for a in audit:
        for b in routes:
            if a['name'] == b['divId']:
                a['type'] = b['id']
                a['value'] = b['name']
    return audit


def deal_templates_html_api(html):
    soup = BeautifulSoup(html, 'lxml')
    data = []
    input_strs = soup.findAll(['div'], attrs={"class": "form-group draggable ui-draggable dropped"})
    # print(input_strs)
    # print(input_strs[0].findAll('input')[0].get('id', ''))
    for input_str in input_strs:
        if len(input_str.findAll('input', attrs={"type": "text"})) > 0:
            # print('readonly', input_str.input.get('readonly'))
            if input_str.input.get('readonly') is None:
                data.append({
                    'name': input_str.label.text,
                    'label_id': input_str.input.get('id', ''),
                    'type': input_str.input.get('type', ''),
                    'choose': []
                })
            else:
                data.append({
                    'name': input_str.label.text,
                    'label_id': input_str.input.get('id', ''),
                    'type': "datetime",
                    'choose': []
                })
        if len(input_str.findAll('select', attrs={"type": "select"})) > 0:
            select_temp = input_str.findAll('select', attrs={"type": "select"})[0]
            # print('select_temp', input_str.findAll('select', attrs={"type": "select"}))
            # if select_temp.get('id'):
            print('label', input_str.label)
            data.append({
                'name': input_str.label.text,
                'label_id': select_temp.get('id', ''),
                'type': select_temp.get('type', ''),
                'choose': [{'label_id': '', 'value': record.text} for record in select_temp.findAll('option')]
            })
        if len(input_str.findAll('input', attrs={"type": "radio"})) > 0:
            # print(input_str.findAll('input', attrs={"type": "radio"}))
            # for radio_temp in input_str.findAll('input', attrs={"type": "radio"}):
            data.append({
                'name': input_str.label.text,
                'label_id': '',
                'type': 'radio',
                'choose': [{'label_id': record.get('id', ''), 'value': record.get('value', '')} for record in
                           input_str.findAll('input', attrs={"type": "radio"})]
            })
        if len(input_str.findAll('input', attrs={"type": "checkbox"})) > 0:
            # print(input_str.findAll('input', attrs={"type": "radio"}))
            # for radio_temp in input_str.findAll('input', attrs={"type": "radio"}):
            if len(input_str.findAll('input', attrs={"type": "checkbox"})) > 1:
                data.append({
                    'name': input_str.label.text,
                    'label_id': '',
                    'type': 'checkbox',
                    'choose': [{'label_id': record.get('id', ''), 'value': record.get('name', '')} for record in
                               input_str.findAll('input', attrs={"type": "checkbox"})]
                })
        if len(input_str.findAll('textarea', attrs={"type": "textarea"})) > 0:
            # print(input_str.findAll('input', attrs={"type": "radio"}))
            # for radio_temp in input_str.findAll('input', attrs={"type": "radio"}):
            data.append({
                'name': input_str.label.text,
                'label_id': input_str.textarea.get('id', ''),
                'type': 'textarea',
                'choose': []
            })
        if len(input_str.findAll('input', attrs={"type": "file"})) > 0:
            # print(input_str.findAll('input', attrs={"type": "radio"}))
            # for radio_temp in input_str.findAll('input', attrs={"type": "radio"}):
            data.append({
                'name': input_str.label.text,
                'label_id': input_str.input.get('id', ''),
                'type': 'upload_file',
                'choose': []
            })
    # print(data)
    return data


def new_flow_node(flow_node):
    routes = flow_node['route']
    lines = flow_node['line']
    routes_list = []
    pageSourceIds = []
    for route in routes:
        routes_list.append({'div_id': route['divId'], 'node_type': route['flowNodeType']})

    for line in lines:
        pageSourceIds.append(line['pageSourceId'])
    audit = []
    cf = []
    n = 1
    for route in routes_list:
        for line in lines:
            if line['pageTargetId'] == route['div_id']:
                if route['div_id'] not in cf:
                    audit.append({'name': route['div_id'], 'num': n, 'node_type': route['node_type']})
                    cf.append(route['div_id'])
                    if pageSourceIds.count(line['pageSourceId']) == 1:
                        n += 1
                    else:
                        pageSourceIds.remove(line['pageSourceId'])
    for a in audit:
        for b in routes:
            if a['name'] == b['divId']:
                a['type'] = b['id']
                a['value'] = b['name']
    return audit



if __name__ == '__main__':
    html = """
     <div class="col-md-12 droppable sortable ui-droppable ui-sortable">
                                            <div class="form-group draggable ui-draggable dropped">
                                            <label class="col-sm-2 control-label">文本：</label><div class="col-sm-7">
                                            <input id="20d1fea6e896616599c1f46c33de5b8a" type="text" class="form-control" placeholder="请输入文本"></div>
                                            </div><div class="form-group draggable ui-draggable dropped">
                                            <label class="col-sm-2 control-label">下拉框：</label><div class="col-sm-7">
                                            <select type="select" id="b1c8dfcb541062719fa6fa1c73586599" class="form-control">
                                            <option>默认1</option><option>默认2</option><option>默认3</option><option>默认4</option></select>
                                            </div><p class="tools col-sm-3"><a class="edit-link" name="select" title="设置">
                                            <i class="fa fa-cog fa-fw"></i></a><a class="remove-link"><i class="fa fa-trash-o"></i></a>
                                            <label class="labcheck"><input class="radio-checkbox" type="checkbox"> 必填</label></p>
                                            </div>
                                            <div class="form-group draggable ui-draggable dropped"><label class="col-sm-2 control-label">单选：</label><div class="col-sm-7">
                                                <label class="radio-inline"><input id="46ca92043c87694cb21e18603a76a9bd" type="radio" name="radio_button" value="默认1" checked=""> 默认1</label>
                                                <label class="radio-inline"><input id="db128a805cec608bb4a674736d4fb4e0" type="radio" name="radio_button" value="默认2"> 默认2</label>
                                                <label class="radio-inline"><input id="d2130d1b984760b389f81f4dc2298ba8" type="radio" name="radio_button" value="默认3"> 默认3</label>
                                                <label class="radio-inline"><input id="e0dadbc4f9446131b7ca8119d3347522" type="radio" name="radio_button" value="默认4"> 默认4</label>
                                            </div>
                                            </div><div class="form-group draggable ui-draggable dropped"><label class="col-sm-2 control-label">多选：</label>
                                            <div class="col-sm-7"><label class="radio-inline_1"><input class="radio-checkbox" id="3f7ac946838c6131ba904130a9319508" type="checkbox" name="默认1"> 
                                            默认1</label><label class="radio-inline_1"><input class="radio-checkbox" id="14826b89772d66c5a1b163a6e8944ac9" type="checkbox" name="默认2"> 
                                            默认2</label><label class="radio-inline_1"><input class="radio-checkbox" id="ba0115b14f706922a314c755cec614bc" type="checkbox" name="默认3"> 
                                            默认3</label><label class="radio-inline_1"><input class="radio-checkbox" id="07597f4c1368671dba59cbf7a2d52a18" type="checkbox" name="默认4"> 
                                            默认4</label></div><p class="tools col-sm-3"><a class="edit-link" name="checkbox" title="设置"><i class="fa fa-cog fa-fw"></i></a><a class="remove-link">
                                            <i class="fa fa-trash-o"></i></a></p></div><div class="form-group draggable ui-draggable dropped"><label class="col-sm-2 control-label">多行文本：</label>
                                            <div class="col-sm-7"><textarea type="textarea" id="2e93c9e478646250bdf05cb0b54a4bf9" class="form-control" placeholder="请输入文本"></textarea>
                                            </div></div><div class="form-group draggable ui-draggable dropped"><label class="col-sm-2 control-label">文本：</label><div class="col-sm-7">
                                            <input id="a5c3063026fb6f8fb2b6d11ce7c2f82d" readonly="" class="form-control form_datetime" type="text" style="border: 1px solid #e5e6e7;"></div>
                                            <p class="tools col-sm-3"><a class="edit-link" name="datetime" title="设置">
                                            <i class="fa fa-cog fa-fw"></i></a><a class="remove-link">
                                            <i class="fa fa-trash-o"></i></a></p></div><div id="5ec8cdeb2e2c63f586fcab2ea557ba0a" class="form-group draggable ui-draggable dropped">
                                            <label class="col-sm-2 control-label">上传：</label><div class="col-sm-7">
                                            <div id="upload_file" style="width: 100%;height: 40px;line-height: 40px;background-color: #F5F5F5;color: #1E90FF;padding-left: 1%;padding-right: 1%;">
                        上传附件<span style="color: red;"></span>
                        <a href="javascript:;" class="file" style="vertical-align: top;margin-top: 5px;float: right;">
                            上传文件
                            <input type="file" name="party_main_body" id="party_main_body" onchange="upload_accessory('file1', 'party_main', 'party_main_body')">
                        </a>
                    </div><table style="width: 100%;">
                        <thead>
                            <tr>
                                <td style="width: 5%;border: #cccccc solid 1px;text-align: center;">序号</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">文件名称</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">文件大小</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">上传时间</td>
                                <td style="width: 20%;border: #cccccc solid 1px;border-left: none;text-align: center;">上传人员</td>
                                <td style="width: 15%;border: #cccccc solid 1px;border-left: none;text-align: center;">操作</td>
                            </tr>
                        </thead>
                        <tbody style="overflow: auto;" id="party_main"></tbody>
                    </table></div></div></div>
    """
    data = deal_templates_html_api(html)
    print(data)
